package com.ojoin.trade.supplyloan_core.web.controller;

import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.web.controller.BaseController;
import com.ojoin.trade.common.web.controller.BaseControllerImpl;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common.web.service.BaseService;
import com.ojoin.trade.supplyloan_core.web.domain.TbCatchdataStrategy;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyInventory;
import com.ojoin.trade.supplyloan_core.web.service.TbCatchdataStrategyService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlPlatformUserService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlSupplyInventoryService;

@Controller
@RequestMapping("/operate/tbslsupplyinventory")
public class TbSlSupplyInventoryController extends
		BaseControllerImpl<TbSlSupplyInventory> implements
		BaseController<TbSlSupplyInventory> {

	private org.slf4j.Logger log = LoggerFactory
			.getLogger(TbSlSupplyInventoryController.class);

	@Autowired
	private TbSlSupplyInventoryService service;

	@Autowired
	private TbSlPlatformUserService tbSlPlatformUserService;

	@Autowired
	private TbCatchdataStrategyService catchdataStrategyService;

	@Override
	protected BaseService<TbSlSupplyInventory> getBaseService() {

		return service;
	}

	/*
	 * 电商业务数据统计
	 */

	@ResponseBody
	@RequestMapping(value = "/querybytbslsupplyinventory", method = RequestMethod.GET)
	public Result queryByTBSLSupplyPayment(int platformAccountId,
			String startDate, String endDate,

			String itemStatus, Pageable page) {
		try { 
			log.info(startDate + "---endDate" + endDate + "-----itemStatus"
					+ itemStatus);
			TbCatchdataStrategy ca = catchdataStrategyService
					.queryByPlatformUserId(platformAccountId);

			List<TbSlSupplyInventory> li = null;

			if (ca.getInventoryCatchAllOrPart().equals(
					TbCatchdataStrategy.CatchAll)) {
				
				if(CommonUtils.isEmpty(startDate)){
					startDate = endDate;
				} 
				li = service.queryInventoryByRowCreateTime(platformAccountId,
						startDate, endDate, itemStatus, page);
				
			} else {
				li = service.queryInventoryByStoreDate(platformAccountId,
						startDate, endDate, itemStatus, page);
			}

			return new Result(Result.Status.OK, li);
		} catch (Exception e) {
			log.error("", e);
			return new Result(Result.Status.ERROR, "查询电商业务数据统计失败");
		}
	}
}
